package com.cltech.controller.sys;

import com.cltech.dto.SysDicDto;
import com.cltech.entity.sys.SysDic;
import com.cltech.frame.result.PageResult;
import com.cltech.frame.result.ResponseBean;
import com.cltech.mapper.SysDicMapper;
import com.cltech.service.sys.SysDicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("api/sysDic")
@Api(tags = { "数据字典" })
public class SysDicController {
	@Autowired
	private SysDicService sysDicService;

	@PostMapping
	@ApiOperation("新增或保存字典")
	public Object addOrUpdate(@RequestBody @Valid SysDicDto sysDicDto, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResponseBean.fail(bindingResult.getFieldError().getDefaultMessage());
		} else {
			return sysDicService.addOrUpdate(sysDicDto);
		}
	}

	@GetMapping
	@ApiOperation("分页查询字典")
	public Object list(HttpServletRequest request, @RequestParam(value="page",defaultValue="0")int page,
					   @RequestParam(value="size",defaultValue="10")int size) {
		return ResponseBean.success(PageResult.result(sysDicService.findAll(request.getParameterMap()
				,page, size)));
	}

	@GetMapping("/{id}")
	@ApiOperation("根据id查询字典")
	public Object get(@PathVariable String id) {
		return sysDicService.findById(id);
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除字典")
	public Object delete(@PathVariable String id) {
		sysDicService.deleteById(id);
		return ResponseBean.success("删除成功");
	}


}
